/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

class BICounter {

    private static AtomicReference<BigInteger> ref = new AtomicReference<>(BigInteger.ONE);

    public static BigInteger next() {
        while (true) {
            BigInteger current = ref.get();
            BigInteger next = current.shiftLeft(1);
            if (ref.compareAndSet(current, next)) {
                return current;
            }
        }
    }
}

class BIThread extends Thread {

    private int n;

    public BIThread(int n) {
        this.n = n;
    }

    @Override
    public void run() {
        for (int i = 0; i < n; i++) {
            BigInteger number = BICounter.next();
            System.out.println(getName() + ": " + number);
        }
    }
}

/**
 *
 * @author Oksana_Moroz
 */
public class Pr4 {

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new BIThread(3).start();
        }
    }
}
